package requests

import (
	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
	"wskyygyso/api/app/requests/validators"
)

type LoginByPhoneRequest struct {
	Phone      string `json:"phone,omitempty" valid:"phone"`
	VerifyCode string `json:"verify_code,omitempty" valid:"verify_code"`
}

func LoginByPhone(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"phone": []string{
			"required", "digits:11",
		},
		"verify_code": []string{
			"required", "digits:6",
		},
	}

	messages := govalidator.MapData{
		"phone": []string{
			"required:手机号码必须设置",
			"digits:手机号码必须是11位数",
		},
		"verify_code": []string{
			"required:验证码必须设置",
			"digits:验证码必须是6位数",
		},
	}
	errs := validate(data, rules, messages)
	_data := data.(*LoginByPhoneRequest)
	errs = validators.ValidateVerifyCode(_data.Phone, _data.VerifyCode, errs)
	return errs
}

type LoginByPasswordRequest struct {
	LoginId     string `json:"login_id" valid:"login_id"`
	PassWord    string `json:"pass_word" valid:"pass_word"`
	CaptchaId   string `json:"captcha_id" valid:"captcha_id"`
	CaptchaCode string `json:"captcha_code" valid:"captcha_code"`
}

func LoginByPassword(data interface{}, c *gin.Context) map[string][]string {
	rules := govalidator.MapData{
		"login_id": []string{
			"required", "min:3",
		},
		"pass_word": []string{
			"required", "min:6",
		},
		"captcha_id": []string{
			"required",
		},
		"captcha_code": []string{
			"required", "digits:6",
		},
	}

	messages := govalidator.MapData{
		"login_id": []string{
			"required:登录 ID 为必填项，支持手机号、邮箱和用户名",
			"min:登录 ID 长度需大于 3",
		},
		"password": []string{
			"required:密码为必填项",
			"min:密码长度需大于 6",
		},
		"captcha_id": []string{
			"required:图片验证码的 ID 为必填",
		},
		"captcha_code": []string{
			"required:图片验证码答案必填",
			"digits:图片验证码长度必须为 6 位的数字",
		},
	}
	errs := validate(data, rules, messages)
	_data := data.(*LoginByPasswordRequest)
	errs = validators.ValidateCaptcha(_data.CaptchaId, _data.CaptchaCode, errs)
	return errs

}
